package com.mm.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mm.entity.MmUser;
import com.mm.service.impl.MmUserServiceImpl;
import com.xkcoding.justauth.AuthRequestFactory;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author wdx
 * @date 2021/1/22 11:06
 */
@Slf4j
@RequestMapping("auth")
@Controller
public class OAuth2Controller {

    @Autowired
    AuthRequestFactory factory;

  /*  @GetMapping
    public List<String> list() {
        return factory.oauthList();
    }*/

    @GetMapping
    public String QQLogin() throws IOException {
        System.out.println("ssss");
        return "login";
    }


    @GetMapping("/login/{type}")
    public void login(@PathVariable String type, HttpServletResponse response) throws IOException {
        AuthRequest authRequest = factory.get(type);
        response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
    }

    @RequestMapping("/{type}/callback")
    public String login(@PathVariable String type, HttpSession session,AuthCallback callback, Model model) {
        AuthRequest authRequest = factory.get(type);
        AuthResponse response = authRequest.login(callback);
        log.info("【response】= {}", JSONUtil.toJsonStr(response));
        if (response.ok()) {
            AuthUser data = (AuthUser) response.getData();
            //已登陆过直接放行
            MmUser user =
                    userService.getOne(new QueryWrapper<MmUser>().lambda().eq(MmUser::getLoginType, type).eq(MmUser::getSocialId, data.getUuid()));
            if (user != null) {
//                model.addAttribute("user",user);
                session.setAttribute("user",user);
                return "index";
            } else {
                //未登录过录入基本信息
                user = new MmUser();
                user.setNickName(data.getNickname());
                user.setHeadImg(data.getAvatar());
                user.setSocialId(data.getUuid());
                user.setLoginType(type);
                userService.save(user);
//                model.addAttribute("user",user);
                session.setAttribute("user",user);
            }
        }
        return "index";

    }


    @RequestMapping("/{type}/cancle")
    public String cancle(@PathVariable String type, HttpSession session,AuthCallback callback, Model model) {

        return "login";

    }

    @Autowired
    MmUserServiceImpl userService;
}
